package com.bj58.mis.apm.server.kylin.common.hllc;

import com.bj58.mis.apm.server.kylin.common.util.BytesUtil;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;

public class HLLCounter implements Serializable, Comparable<HLLCounter> {
    static double OVERFLOW_FACTOR = 0.01D;
    private int p;
    private int m;
    private HashFunction hashFunc;
    private Register register;

    public HLLCounter() {
        this(10, RegisterType.SINGLE_VALUE, Hashing.murmur3_128());
    }

    public HLLCounter(int p) {
        this(p, RegisterType.SINGLE_VALUE, Hashing.murmur3_128());
    }

    public HLLCounter(int p, HashFunction hashFunc) {
        this(p, RegisterType.SINGLE_VALUE, hashFunc);
    }

    public HLLCounter(HLLCounter another) {
        this(another.p, another.getRegisterType(), another.hashFunc);
        this.merge(another);
    }

    public HLLCounter(int p, RegisterType type) {
        this(p, type, Hashing.murmur3_128());
    }

    HLLCounter(int p, RegisterType type, HashFunction hashFunc) {
        this.hashFunc = Hashing.murmur3_128();
        this.p = p;
        this.m = 1 << p;
        this.hashFunc = hashFunc;
        if (type == RegisterType.SINGLE_VALUE) {
            this.register = new SingleValueRegister();
        } else if (type == RegisterType.SPARSE) {
            this.register = new SparseRegister();
        } else {
            this.register = new DenseRegister(p);
        }

    }

    private boolean isDense(int size) {
        double over = OVERFLOW_FACTOR * (double)this.m;
        return size > (int)over;
    }

    public void add(int value) {
        this.add(this.hashFunc.hashInt(value).asLong());
    }

    public void add(String value) {
        this.add(this.hashFunc.hashString(value, Charset.defaultCharset()).asLong());
    }

    public void add(byte[] value) {
        this.add(this.hashFunc.hashBytes(value).asLong());
    }

    public void add(byte[] value, int offset, int length) {
        this.add(this.hashFunc.hashBytes(value, offset, length).asLong());
    }

    public void addHashDirectly(long hash) {
        this.add(hash);
    }

    protected void add(long hash) {
        int bucketMask = this.m - 1;
        int bucket = (int)(hash & (long)bucketMask);
        int firstOnePos = Long.numberOfLeadingZeros(hash | (long)bucketMask) + 1;
        if (this.register.getRegisterType() == RegisterType.SINGLE_VALUE) {
            SingleValueRegister sr = (SingleValueRegister)this.register;
            int pos = sr.getSingleValuePos();
            if (pos >= 0 && pos != bucket) {
                this.register = sr.toSparse();
                this.setIfBigger(this.register, bucket, (byte)firstOnePos);
            } else {
                this.setIfBigger(this.register, bucket, (byte)firstOnePos);
            }
        } else {
            this.setIfBigger(this.register, bucket, (byte)firstOnePos);
            this.toDenseIfNeeded();
        }

    }

    private void setIfBigger(Register register, int pos, byte value) {
        byte b = register.get(pos);
        if (value > b) {
            register.set(pos, value);
        }

    }

    private void toDenseIfNeeded() {
        if (this.register.getRegisterType() == RegisterType.SPARSE && this.isDense(this.register.getSize())) {
            this.register = ((SparseRegister)this.register).toDense(this.p);
        }

    }

    public void merge(HLLCounter another) {
        assert this.p == another.p;

        assert this.hashFunc == another.hashFunc;

        label38:
        switch(this.register.getRegisterType()) {
            case SINGLE_VALUE:
                switch(another.getRegisterType()) {
                    case SINGLE_VALUE:
                        if (this.register.getSize() > 0 && another.register.getSize() > 0) {
                            this.register = ((SingleValueRegister)this.register).toSparse();
                            break label38;
                        }

                        SingleValueRegister sr = (SingleValueRegister)another.register;
                        if (sr.getSize() > 0) {
                            this.register.set(sr.getSingleValuePos(), sr.getValue());
                        }

                        return;
                    case SPARSE:
                        this.register = ((SingleValueRegister)this.register).toSparse();
                        break label38;
                    case DENSE:
                        this.register = ((SingleValueRegister)this.register).toDense(this.p);
                    default:
                        break label38;
                }
            case SPARSE:
                if (another.getRegisterType() == RegisterType.DENSE) {
                    this.register = ((SparseRegister)this.register).toDense(this.p);
                }
        }

        this.register.merge(another.register);
        this.toDenseIfNeeded();
    }

    public long getCountEstimate() {
        return (new HLLCounter.HLLCSnapshot(this)).getCountEstimate();
    }

    public int getPrecision() {
        return this.p;
    }

    public double getErrorRate() {
        return 1.04D / Math.sqrt((double)this.m);
    }

    public String toString() {
        return "" + this.getCountEstimate();
    }

    public static void main(String[] args) throws IOException {
        dumpErrorRates();
    }

    static void dumpErrorRates() {
        for(int p = 10; p <= 18; ++p) {
            double rate = (new HLLCounter(p, RegisterType.SPARSE)).getErrorRate();
            double er = (double)Math.round(rate * 10000.0D) / 100.0D;
            double er2 = (double)Math.round(rate * 2.0D * 10000.0D) / 100.0D;
            double er3 = (double)Math.round(rate * 3.0D * 10000.0D) / 100.0D;
            long size = Math.round(Math.pow(2.0D, (double)p));
            System.out.println("HLLC" + p + ",\t" + size + " bytes,\t68% err<" + er + "%" + ",\t95% err<" + er2 + "%" + ",\t99.7% err<" + er3 + "%");
        }

    }

    public Register getRegister() {
        return this.register;
    }

    public void clear() {
        this.register.clear();
    }

    public void writeRegisters(ByteBuffer out) throws IOException {
        int indexLen = this.getRegisterIndexSize();
        int size = this.register.getSize();
        byte scheme;
        if (!(this.register instanceof SingleValueRegister) && !(this.register instanceof SparseRegister) && 5 + (indexLen + 1) * size >= this.m) {
            scheme = 1;
        } else {
            scheme = 0;
        }

        out.put(scheme);
        if (scheme == 0) {
            BytesUtil.writeVInt(size, out);
            if (this.register.getRegisterType() == RegisterType.SINGLE_VALUE) {
                if (size > 0) {
                    SingleValueRegister sr = (SingleValueRegister)this.register;
                    writeUnsigned(sr.getSingleValuePos(), indexLen, out);
                    out.put(sr.getValue());
                }
            } else if (this.register.getRegisterType() == RegisterType.SPARSE) {
                Collection<Entry<Integer, Byte>> allValue = ((SparseRegister)this.register).getAllValue();
                Iterator i$ = allValue.iterator();

                while(i$.hasNext()) {
                    Entry<Integer, Byte> entry = (Entry)i$.next();
                    writeUnsigned((Integer)entry.getKey(), indexLen, out);
                    out.put((Byte)entry.getValue());
                }
            } else {
                byte[] registers = ((DenseRegister)this.register).getRawRegister();

                for(int i = 0; i < this.m; ++i) {
                    if (registers[i] > 0) {
                        writeUnsigned(i, indexLen, out);
                        out.put(registers[i]);
                    }
                }
            }
        } else {
            if (scheme != 1) {
                throw new IllegalStateException();
            }

            out.put(((DenseRegister)this.register).getRawRegister());
        }

    }

    public void readRegisters(ByteBuffer in) throws IOException {
        byte scheme = in.get();
        if (scheme == 0) {
            this.clear();
            int size = BytesUtil.readVInt(in);
            if (size > this.m) {
                throw new IllegalArgumentException("register size (" + size + ") cannot be larger than m (" + this.m + ")");
            }

            if (this.isDense(size)) {
                this.register = new DenseRegister(this.p);
            } else if (size <= 1) {
                this.register = new SingleValueRegister();
            } else {
                this.register = new SparseRegister();
            }

            int indexLen = this.getRegisterIndexSize();
            int key;

            for(int i = 0; i < size; ++i) {
                key = readUnsigned(in, indexLen);
                this.register.set(key, in.get());
            }
        } else {
            if (scheme != 1) {
                throw new IllegalStateException();
            }

            if (this.register.getRegisterType() != RegisterType.DENSE) {
                this.register = new DenseRegister(this.p);
            }

            in.get(((DenseRegister)this.register).getRawRegister());
        }

    }

    public int peekLength(ByteBuffer in) {
        int mark = in.position();
        byte scheme = in.get();
        int len;
        if (scheme == 0) {
            int size = BytesUtil.readVInt(in);
            int indexLen = this.getRegisterIndexSize();
            len = in.position() - mark + (indexLen + 1) * size;
        } else {
            len = in.position() - mark + this.m;
        }

        in.position(mark);
        return len;
    }

    public int maxLength() {
        return 1 + this.m;
    }

    private int getRegisterIndexSize() {
        return (this.p - 1) / 8 + 1;
    }

    public int hashCode() {
        int result = 1;
        result = 31 * result + (this.hashFunc == null ? 0 : this.hashFunc.hashCode());
        result = 31 * result + this.p;
        result = 31 * result + this.register.hashCode();
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (this.getClass() != obj.getClass()) {
            return false;
        } else {
            HLLCounter other = (HLLCounter)obj;
            if (!this.hashFunc.equals(other.hashFunc)) {
                return false;
            } else if (this.p != other.p) {
                return false;
            } else {
                return this.register.equals(other.register);
            }
        }
    }

    public int compareTo(HLLCounter o) {
        if (o == null) {
            return 1;
        } else {
            long e1 = this.getCountEstimate();
            long e2 = o.getCountEstimate();
            if (e1 == e2) {
                return 0;
            } else {
                return e1 > e2 ? 1 : -1;
            }
        }
    }

    public static void writeUnsigned(int num, int size, ByteBuffer out) {
        for(int i = 0; i < size; ++i) {
            out.put((byte)num);
            num >>>= 8;
        }

    }

    public static int readUnsigned(ByteBuffer in, int size) {
        int integer = 0;
        int mask = 255;
        int shift = 0;

        for(int i = 0; i < size; ++i) {
            integer |= in.get() << shift & mask;
            mask <<= 8;
            shift += 8;
        }

        return integer;
    }

    public RegisterType getRegisterType() {
        return this.register.getRegisterType();
    }

    public static class HLLCSnapshot {
        byte p;
        double registerSum;
        int zeroBuckets;

        public HLLCSnapshot(HLLCounter hllc) {
            this.p = (byte)hllc.p;
            this.registerSum = 0.0D;
            this.zeroBuckets = 0;
            Register register = hllc.getRegister();
            DenseRegister dr;
            if (register.getRegisterType() == RegisterType.SINGLE_VALUE) {
                dr = ((SingleValueRegister)register).toDense(this.p);
            } else if (register.getRegisterType() == RegisterType.SPARSE) {
                dr = ((SparseRegister)register).toDense(this.p);
            } else {
                dr = (DenseRegister)register;
            }

            byte[] registers = dr.getRawRegister();

            for(int i = 0; i < hllc.m; ++i) {
                if (registers[i] == 0) {
                    ++this.registerSum;
                    ++this.zeroBuckets;
                } else {
                    this.registerSum += 1.0D / (double)(1L << registers[i]);
                }
            }

        }

        public long getCountEstimate() {
            int m = 1 << this.p;
            double alpha = 0.7213D / (1.0D + 1.079D / (double)m);
            double estimate = alpha * (double)m * (double)m / this.registerSum;
            if ((double)this.zeroBuckets >= (double)m * 0.07D) {
                estimate = (double)m * Math.log((double)m * 1.0D / (double)this.zeroBuckets);
            } else if (HyperLogLogPlusTable.isBiasCorrection(m, estimate)) {
                estimate = HyperLogLogPlusTable.biasCorrection(this.p, estimate);
            }

            return Math.round(estimate);
        }
    }
}

